import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

if __name__ == '__main__':
    """宣讲会举办数量折线图（按周）"""
    df = pd.read_csv('../teachin_20241101_20231101.csv')
    df['date'] = df['date'].apply(lambda x: x.split(' ')[0])
    df['date'] = pd.to_datetime(df['date'])
    df['browse'] = df['browse'].apply(lambda x: x.split('：')[1])
    df['browse'] = pd.to_numeric(df['browse'])
    df['week'] = df['date'] - pd.to_timedelta(df['date'].dt.weekday, unit='D')
    df['week'] = df['week'].dt.date
    df = df[df['date'] >= '2023-11-01']
    df = df[df['date'] < '2024-11-01']
    grouped = df.groupby('week')
    teachin = grouped['title'].count()
    browse = grouped['browse'].sum()
    full_date_range = pd.date_range(start='2023-11-01', end='2024-11-01', freq='W-MON')
    teachin = teachin.reindex(full_date_range).fillna(0)
    browse = browse.reindex(full_date_range).fillna(0)
    plt.figure()
    matplotlib.rcParams['font.sans-serif'] = 'KaiTi'
    plt.plot(teachin.index, teachin, 'o-b', label='宣讲会')
    plt.ylabel('数量（一周）')
    plt.legend(loc='upper left')
    plt.twinx()
    plt.plot(browse.index, browse, '.--r', label='点击量')
    plt.ylabel('点击量（一周）')
    plt.legend(loc='upper right')
    plt.title('宣讲会举办数量')
    plt.xlabel('时间')
    plt.grid()
    plt.show()
